package colorChanger;

import java.awt.Color;

public class RGBtoCMYK {
	public final static CMYK MAGENTA = new CMYK(0.0, 1.0, 0.0, 0.0);
	public final static CMYK YELLOW = new CMYK(0.0, 0.0, 1.0, 0.0);
	public final static CMYK CYAN = new CMYK(1.0, 0.0, 0.0, 0.0);
	public final static CMYK KEY = new CMYK(0.0, 0.0, 0.0, 1.0);
	public final static int MAGENTA_PIXEL = -65282;
	public final static int YELLOW_PIXEL = -256;
	// public final static int YELLOW_PIXEL = Color.white.getRGB();
	// TODO find yellow pixel
	public final static int CYAN_PIXEL = -16711681;
	public final static int KEY_PIXEL = -16777216;
	public final static int WHITE_PIXEL = -1;

	public static CMYK convert(int rgbColor) {
		Color color = new Color(rgbColor);
		double r = color.getRed();
		double g = color.getGreen();
		double b = color.getBlue();
		double computedC = 0;
		double computedM = 0;
		double computedY = 0;
		double computedK = 0;

		if (r == 0 && g == 0 && b == 0) {
			computedK = 1;
			return new CMYK(0, 0, 0, 1);
		}

		computedC = 1 - (r / 255);
		computedM = 1 - (g / 255);
		computedY = 1 - (b / 255);

		double minCMY = Math.min(computedC, Math.min(computedM, computedY));
		computedC = (computedC - minCMY) / (1 - minCMY);
		computedM = (computedM - minCMY) / (1 - minCMY);
		computedY = (computedY - minCMY) / (1 - minCMY);
		computedK = minCMY;

		return new CMYK(computedC, computedM, computedY, computedK);
	}

	public static CMYK convert(double r, double g, double b) {
		double computedC = 0;
		double computedM = 0;
		double computedY = 0;
		double computedK = 0;

		if (r == 0 && g == 0 && b == 0) {
			computedK = 1;
			return new CMYK(0, 0, 0, 1);
		}

		computedC = 1 - (r / 255);
		computedM = 1 - (g / 255);
		computedY = 1 - (b / 255);

		double minCMY = Math.min(computedC, Math.min(computedM, computedY));
		computedC = (computedC - minCMY) / (1 - minCMY);
		computedM = (computedM - minCMY) / (1 - minCMY);
		computedY = (computedY - minCMY) / (1 - minCMY);
		computedK = minCMY;

		return new CMYK(computedC, computedM, computedY, computedK);
	}

	public static int convertAbsoluteValue(int RGB) {
		Color color = new Color(RGB);
		CMYK cmyk = convert(color.getRed(), color.getGreen(), color.getBlue());
		// int colorIntensity;
		if (isCyan(RGB)) {
			return RGBtoCMYK.CYAN_PIXEL;
		}
		if(isMagenta(RGB)){
			return RGBtoCMYK.MAGENTA_PIXEL;
		}
		if(isYellow(RGB)){
			return RGBtoCMYK.YELLOW_PIXEL;
		}
		return RGBtoCMYK.KEY_PIXEL;
		// if (cmyk.getC().getAbsoluteIntensity() >= CMYKColor.STRONG) {
		// return RGBtoCMYK.CYAN_PIXEL;
		//
		// } else if (cmyk.getM().getAbsoluteIntensity() >= CMYKColor.STRONG) {
		// return RGBtoCMYK.MAGENTA_PIXEL;
		//
		// } else if (cmyk.getY().getAbsoluteIntensity() >= CMYKColor.STRONG) {
		// return RGBtoCMYK.YELLOW_PIXEL;
		// } else if (cmyk.getK().getAbsoluteIntensity() >= CMYKColor.STRONG) {
		// return RGBtoCMYK.KEY_PIXEL;
		// }

//		return RGBtoCMYK.WHITE_PIXEL;
	}
	public static boolean isWhite(int rgb) {
		CMYK maybeWhyte = convert(rgb);
		
		if(!(maybeWhyte.getC().getIniticalColorIntensity()<0.4)){
			return false;
		}
		if(!(maybeWhyte.getM().getIniticalColorIntensity()<0.4)){
			return false;
		}
		if(!(maybeWhyte.getY().getIniticalColorIntensity()<0.4)){
			return false;
		}
		if(!(maybeWhyte.getK().getIniticalColorIntensity()<0.5)){
			return false;
		}
		return true;
	}

	public static boolean isYellow(int rgb) {
		CMYK maybeYellow = convert(rgb);
		
		if(!(maybeYellow.getC().getIniticalColorIntensity()<0.55)){
			return false;
		}
		if(!(maybeYellow.getM().getIniticalColorIntensity()<0.2)){
			return false;
		}
		if(!(maybeYellow.getY().getIniticalColorIntensity()>0.3)){
			return false;
		}
		if(!(maybeYellow.getK().getIniticalColorIntensity()<0.60)){
			return false;
		}
		return true;
	}

	public static boolean isMagenta(int rgb) {
		CMYK maybeMagenta = convert(rgb);
		if(!(maybeMagenta.getC().getIniticalColorIntensity()<0.3)){
			return false;
		}
		if(!(maybeMagenta.getM().getIniticalColorIntensity()>0.3)){
			return false;
		}
		if(!(maybeMagenta.getY().getIniticalColorIntensity()<0.6)){
			return false;
		}
		if(!(maybeMagenta.getK().getIniticalColorIntensity()<0.7)){
			return false;
		}
		return true;
	}

	public static boolean isCyan(int rgb) {
		CMYK maybeCyan = convert(rgb);
		if (!(maybeCyan.getC().getIniticalColorIntensity() > 0.3)) {
			return false;
		}
		if (!(maybeCyan.getM().getIniticalColorIntensity() < 0.6)) {
			return false;
		}
		if (!(maybeCyan.getY().getIniticalColorIntensity() < 0.6)) {
			return false;
		}
		if (!(maybeCyan.getK().getIniticalColorIntensity() < 0.8)) {
			return false;
		}
		return true;
	}
}
